package com.tgy.core.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tgy.core.mapper.SysPermissionMapper;
import com.tgy.core.service.SysPermissionService;
import com.tgy.page.PageQuery;
import com.tgy.page.TableDataInfo;
import com.tgy.pojo.SysPermission;
import com.tgy.result.LayuiTableResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

/**
 * 权限信息Service业务层处理
 *
 * @author 自动生成
 * @date 2022-06-20
 */
@Service
public class SysPermissionServiceImpl extends ServiceImpl<SysPermissionMapper, SysPermission> implements SysPermissionService {

    @Resource
    private SysPermissionMapper baseMapper;

    /**
     * 查询权限信息
     *
     * @param id 权限信息主键
     * @return 权限信息
     */
    @Override
    public SysPermission queryById(String id){
        return baseMapper.selectById(id);
    }

    /**
     * 根据Id集合查询
     * @param ids   ids
     * @return
     */
    @Override
    public List<SysPermission> queryByIds(List<String> ids) {
        return baseMapper.selectBatchIds(ids);
    }

    /**
     * 查询权限信息列表
     *
     * @param bo 权限信息
     * @return 权限信息
     */
    @Override
    public TableDataInfo<SysPermission> queryPageList(SysPermission bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysPermission> lqw = buildQueryWrapper(bo);
        Page<SysPermission> result = baseMapper.selectPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public LayuiTableResult queryLayuiPageList(SysPermission bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysPermission> lqw = buildQueryWrapper(bo);
        Page<SysPermission> result = baseMapper.selectPage(pageQuery.build(), lqw);
        return new LayuiTableResult(result.getTotal(),result.getRecords());
    }

    /**
     * 查询权限信息列表
     *
     * @param bo 权限信息
     * @return 权限信息
     */
    @Override
    public List<SysPermission> queryList(SysPermission bo) {
        LambdaQueryWrapper<SysPermission> lqw = buildQueryWrapper(bo);
        return baseMapper.selectList(lqw);
    }

    private LambdaQueryWrapper<SysPermission> buildQueryWrapper(SysPermission bo) {
        LambdaQueryWrapper<SysPermission> lqw = Wrappers.lambdaQuery();
        lqw.eq(Objects.nonNull(bo.getAvailable()),SysPermission::getAvailable,bo.getAvailable());
        lqw.eq(Objects.nonNull(bo.getCate()),SysPermission::getCate,bo.getCate());
        lqw.eq(Objects.nonNull(bo.getPid()),SysPermission::getPid,bo.getPid());
        lqw.eq(Objects.nonNull(bo.getTitle()),SysPermission::getTitle,bo.getTitle());
        lqw.orderByAsc(SysPermission::getSort);
        return lqw;
    }

    /**
     * 新增权限信息
     *
     * @param bo 权限信息
     * @return 结果
     */
    @Override
    public Boolean insertByBo(SysPermission bo) {
        // validEntityBeforeSave(SysPermission);
        boolean flag = baseMapper.insert(bo) > 0;
        if (flag) {
            bo.setId(bo.getId());
        }
        return flag;
    }

    /**
     * 修改权限信息
     *
     * @param bo 权限信息
     * @return 结果
     */
    @Override
    public Boolean updateByBo(SysPermission bo) {
        // PBizPatient update = BeanUtil.toBean(bo, PBizPatient.class);
        // validEntityBeforeSave(SysPermission);
        return baseMapper.updateById(bo) > 0;
    }

    @Override
    public Boolean deleteById(String id) {
        return baseMapper.deleteById(id) > 0;
    }


    /**
     * 批量删除权限信息
     *
     * @param ids 需要删除的权限信息主键
     * @return 结果
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        /*if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }*/
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public int updateStatus(String id, Integer status) {
        return baseMapper.updateStatus(id,status);
    }


}
